package.path = package.path ..';../?.lua'
local publish = require('publish/publish')
_G.EXPORT_ASSERT_TO_GLOBALS = true
require('lib/luaunit')
tool = require('lib/tool')
_G.testPublish = {}


function testPublish:setUp()
    self.msg_center = publish.provider:New()
    -- self.publisher = publish.publisher:New()
    -- self.publisher:addMsgCenter(self.msg_center)
end

function testPublish:test_scribeAndPublish_base()
    local message_center = publish.provider:New()
    local publisher = publish.publisher:New()
    publisher:addMsgCenter(message_center)


    local sendTime = os.time()
    local msg = { a ="1";b = 2}
    local msg1 = publish.Msg:New(33,publish.msgType.Skill,msg)
    local msg2 = publish.Msg:New(222,publish.msgType.Skill)
    local msg3 = publish.Msg:New(0,publish.msgType.Skill)

    local jim = publish.subscriber:New('jim',message_center)
    jim:subscribe(publish.msgType.Skill)
    jim.dwGetMsgCount = 0
    jim.getMsg = function(aSubscriber, msg)
        jim.dwGetMsgCount = jim.dwGetMsgCount + 1
    end

    publisher:publish(msg1)
    publisher:publish(msg2)
    publisher:publish(msg3)
    local tStart = os.time()
    local tInit = os.time()
    -- while tInit - tStart < 1 do
    --     local tNow = os.time()
    --     if tNow - tInit > 0.1 then
    --         tInit = tNow
    --         message_center:update(tNow)
    --     end
    -- end
     for i=1 ,10000 do
        local tNow = tStart + i
        if tNow - tInit > 1 then
            tInit = tNow
            message_center:update(tNow, timeInterval)
        end
    end
    assertEquals(jim.dwGetMsgCount,3)
end

function test_scribeAndPublish_msg_number_max()
    local message_center = publish.provider:New()
    local publisher = publish.publisher:New()
    publisher:addMsgCenter(message_center)


    local sendTime = os.time()
    local msg = { a ="1";b = 2}
    local msg1 = publish.Msg:New(33,publish.msgType.Skill,msg)
    local msg2 = publish.Msg:New(222,publish.msgType.Skill)
    local msg3 = publish.Msg:New(0,publish.msgType.Skill)


    local jim = publish.subscriber:New('jim',message_center)
    jim:subscribe(publish.msgType.Skill)
    jim.dwGetMsgCount = 0
    jim.getMsg = function(aSubscriber, msg)
        jim.dwGetMsgCount = jim.dwGetMsgCount + 1

    end
    local tStart = os.time()
    publisher:publish(msg1)
    publisher:publish(msg2)
    publisher:publish(msg3)
    math.randomseed(os.time())
    local msgCount = 1000
    for i=1,msgCount do
        local randomTime = math.random(msgCount)
        local msg = publish.Msg:New(randomTime,publish.msgType.Skill)
        publisher:publish(msg)
    end

    local tInit = os.time()
    print("msg count:"..msgCount.."  time spent:"..(tInit - tStart))
    -- while tInit - tStart < 1 do
    --     local tNow = os.time()
    --     if tNow - tInit > 0.1 then
    --         tInit = tNow
    --         message_center:update(tNow)
    --     end
    -- end
 
    -- for i=1 ,10000 do
    --     local tNow = tStart + i
    --     if tNow - tInit > 1 then
    --         tInit = tNow
    --         message_center:update(tNow, timeInterval)
    --     end
    -- end
    -- assertEquals(jim.dwGetMsgCount,12)
end


function testPublish:test_rankQueue_menber_count_max()
    local tStart = os.time()
   local rankQueue = tool.rankQueue:New()
    math.randomseed(os.time())
    local msgCount = 100000
    for i=1,msgCount do
        local randomTime = math.random(msgCount)
        local msg = publish.Msg:New(randomTime,publish.msgType.Skill)
        rankQueue:insert(msg)
    end
    local tInit = os.time()
    print("msg count:"..msgCount.."  time spent:"..(tInit - tStart))
end


local lu = LuaUnit.new()
lu:setOutputType("tap")
os.exit( lu:runSuite() )
